home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-27 | 2.1 KB | 83 lines | [TEXT/CWIE] |
- //
- // CCameraMover.cp
- //
- // class CCameraMover
- // Moves a camera given changes in spherical coordinate values.
- //
- // by James Jennings
- // November 26, 1995
- //
-
- #include "CCameraMover.h"
- #include "QD3D Debug Macros.h"
-
- // the about to avoid the z-axis by so our math won't explode
- const float zAvoid = kQ3Pi/18; // 10 degrees
-
- CCameraMover::CCameraMover(TQ3ViewObject aView)
- {
- ThrowIfQ3Fail_( ::Q3View_GetCamera(aView, &mCamera) );
- TQ3Status status = ::Q3Camera_GetPlacement(mCamera, &mPlacement);
- if (status==kQ3Failure) {
- ::Q3Object_Dispose(mCamera);
- ThrowIfQ3Fail_(status);
- }
- #if 0 // Note: the debugging version of QD3D is wrong here.
- ::Q3Point3D_ToSpherical(&(mPlacement.cameraLocation), &mLocation);
- #else // so we supply an alternate definition
- TQ3Vector3D *v = (TQ3Vector3D*)&(mPlacement.cameraLocation);
- mLocation.rho = ::Q3Vector3D_Length(v);
- if (mLocation.rho==0) {
- mLocation.phi = 0;
- mLocation.theta = 0;
- } else {
- mLocation.phi = ::acos(v->z/mLocation.rho);
- if (v->x==0 && v->y==0)
- mLocation.theta = 0;
- else
- mLocation.theta = ::atan2(v->y, v->x);
- }
- #endif
- }
-
- CCameraMover::~CCameraMover()
- {
- if (mCamera)
- ::Q3Object_Dispose(mCamera);
- }
-
- void
- CCameraMover::AdjustUpVector()
- {
- TQ3Vector3D up, tmp, *loc;
- ::Q3Vector3D_Set(&up, 0, 0, 1);
- loc = (TQ3Vector3D*)&(mPlacement.cameraLocation); // type casting pointer
- // gs = Gramm-Scmit orthogonalization factor
- float gs = ::Q3Vector3D_Dot(loc, &up) / ::Q3Vector3D_Length(loc);
- ::Q3Vector3D_Subtract(
- &up,
- ::Q3Vector3D_Scale(loc, gs, &tmp),
- &up);
- ::Q3Vector3D_Normalize(&up, &up);
- // check for nil vector
-
- mPlacement.upVector = up;
- }
-
- void
- CCameraMover::Move(float deltaTheta, float deltaPhi)
- {
- mLocation.theta += deltaTheta;
- mLocation.phi += deltaPhi;
- // clip the result: 0+ < phi < 180-, 0 < theta < 360
- if (mLocation.phi > kQ3Pi-zAvoid) mLocation.phi = kQ3Pi-zAvoid;
- if (mLocation.phi < 0+zAvoid) mLocation.phi = zAvoid;
- mLocation.theta = ::fmod(mLocation.theta+kQ32Pi, kQ32Pi);
- // store it
- ::Q3SphericalPoint_ToPoint3D(&mLocation, &(mPlacement.cameraLocation));
- AdjustUpVector();
-
- ThrowIfQ3Fail_( ::Q3Camera_SetPlacement(mCamera, &mPlacement) );
- }
-
-